<?php
/**
 * Created by PhpStorm.
 * UserModel: Administrator
 * Date: 2019/3/01/0001
 * Time: 16:59
 */
namespace app\caidongweb\model;
use think\Db;

/**
 * Class AuthGroup
 * 用户组管理类
 * @package app\caidongweb\Model
 */
class AuthGroup extends BaseModel
{
    protected $table = 'cd_auth_group';
    /*
     * function getList
     * 获取用户组列表
     *
     * @return object
     * */
    public function getList($where = '')
    {
        $list = $this->where($where)->select();
        return $list;
    }

    /**
     * function index
     * 用户组首页
     *
     * @return object
     */
    public function index()
    {
        $list = $this->paginate(10,false,['query'=>request()->param()]);
        return $list;
    }
    /**
     * function add
     * 添加用户组
     *
     * @return array
     * */
    public function add()
    {
        if(input('post.')){
            $data = input('post.');
            if($data['title']==''){
                return $this->returnData(-1,'用户组名不能为空');
            }
            if(!empty($data['parentId']) || !empty($data['childId'])){
                if(empty($data['parentId'])){
                    return $this->returnData(-1,'请选择父权限');
                }
                if(empty($data['childId'])){
                    return $this->returnData(-1,'请选择子权限');
                }
                $arr = array_merge ( $data['parentId'] , $data['childId'] );
                $rules = implode(',',$arr);
                $data['rules'] = $rules;
            }
            $res = $this->allowField(true)->save($data);
            if($res){
                return $this->returnData(1,'添加成功');
            }else{
                return $this->returnData(-1,'添加失败');
            }
        }else{
            return $this->fetch();
        }
    }
    /**
     * function edit
     * 用户组权限编辑
     *
     * @return array
     * */
    public function edit()
    {
        $id = input('id');
        if(input('post.')){
            $data = input('post.');
            if(!empty($data['parentId']) || !empty($data['childId'])){
                if(empty($data['parentId'])){
                    return $this->returnData(-1,'请选择父权限');
                }
                if(empty($data['childId'])){
                    return $this->returnData(-1,'请选择子权限');
                }
                $arr = array_merge ( $data['parentId'] , $data['childId'] );
                $rules = implode(',',$arr);
                $res = $this->where('id',$id)->update(['rules'=>$rules,'title'=>$data['title']]);
            }else{
                $res = $this->where('id',$id)->update(['rules'=>'']);
            }
            if($res!==false){
                return $this->returnData(1,'编辑成功');
            }else{
                return $this->returnData(-1,'编辑失败');
            }
        }
    }
    /**
     * function getInfo
     * 获取单条信息详情
     *
     * @return object
     * */
    public function getInfo(){
        $id = input('id');
        $data = $this->where('id',$id)->find();
        return $data;
    }
    /**
     * function updateStatus
     * 编辑状态
     *
     * @return array
     */
    public function updateStatus()
    {
        $data = input('post.');
        $result = $this->where('id',$data['id'])->update(['status'=>$data['status']]);
        if($result){
            return $this->returnData(1,'编辑成功');
        }else{
            return $this->returnData(-1,'编辑失败');
        }
    }

}